package com.ebai.product.management.service.system;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ebai.product.management.model.entity.OperLog;
import com.ebai.product.management.repository.system.OperLogRepository;

/**
 * 操作日志业务处理类
 * @author mgli
 *
 */
@Service
public class OperLogService {

	@Autowired
	private OperLogRepository operLogRepository;
	
	/**
     * 依据条件查询分页数据
     * @param page
     * @param size
     * @param authQuery
     * @return
     */
    public Page<OperLog> queryPage(Integer page, Integer size, final OperLog operLogQuery) {
        Pageable pageable = PageRequest.of(page-1, size, Sort.Direction.DESC, "operTime");
        Page<OperLog> rolePage = this.operLogRepository.findAll(new Specification<OperLog>(){
            public Predicate toPredicate(Root<OperLog> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                
                // 过滤模块标题
                if ( StringUtils.isNotBlank(operLogQuery.getTitle()) ){
                    list.add(criteriaBuilder.like(root.get("title").as(String.class), "%"+ operLogQuery.getTitle() +"%"));
                }
                // 过滤登录账号
                if ( StringUtils.isNotBlank(operLogQuery.getLoginName()) ){
                    list.add(criteriaBuilder.equal(root.get("loginName").as(String.class), "%"+ operLogQuery.getLoginName() +"%"));
                }
                // 过滤操作状态
                if ( StringUtils.isNotBlank(operLogQuery.getStatus()) ){
                    list.add(criteriaBuilder.equal(root.get("status").as(String.class), operLogQuery.getStatus()));
                }

                // 过滤创建时间条件
                if ( StringUtils.isNotBlank(operLogQuery.getCreateTimeBetween()) ){
                    String[] operTime = operLogQuery.getCreateTimeBetween().split(" ~ ");
                    list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("operTime").as(Timestamp.class), Timestamp.valueOf(operTime[0] + " 00:00:00")));
                    list.add(criteriaBuilder.lessThanOrEqualTo(root.get("operTime").as(Timestamp.class), Timestamp.valueOf(operTime[1] + " 23:59:59")));
                }
                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }
        }, pageable);

        return rolePage;
    }
}
